# create_calibration.py
import cv2
from app.model.lane_calibrator import LaneCalibrator

def run_calibration(reference_image_path):
    ref_image = cv2.imread(reference_image_path)
    ref_image = cv2.resize(ref_image, (1920, 1020))
    if ref_image is None:
        print(f"无法加载参考图像: {reference_image_path}")
        return False

    calibrator = LaneCalibrator(bird_view_size=(1920, 1020))
    if calibrator.calibrate(reference_image=ref_image):
        calibration_file = '../calibration/lane_calibration.json'
        if calibrator.save_calibration(calibration_file):
            print(f"校准结果已保存到: {calibration_file}")
            # vis_image = calibrator.visualize_calibration(background=ref_image)
            # cv2.imshow("Calibration Result", vis_image)
            # cv2.waitKey(0)
            cv2.destroyAllWindows()
            return True
        else:
            print("保存校准结果失败")
            return False
    else:
        print("校准失败")
        return False

if __name__ == "__main__":
    reference_image_path = '../../videos/first_frame.jpg'
    run_calibration(reference_image_path)
